#include <jni.h>
#include <string>
#include "utils.cpp"
#include "android/log.h"
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,__VA_ARGS__ ,__VA_ARGS__) // 定义LOGE类型
using namespace std;
extern "C"
JNIEXPORT jobject JNICALL
Java_com_itfitness_cppdemoone_activity_BoundaryFillActivity_imageBoundaryFill(JNIEnv *env,
                                                                              jobject thiz,
                                                                              jobject bitmap,jobject bitmapDest) {
    //源图像
    Mat src;
    //将Bitmap转换为Mat
    BitmapToMat(env,bitmap,src, JNI_FALSE);

    //将四周边界都定为宽度50
    copyMakeBorder(src,src,50,50,50,50,BORDER_REPLICATE);

    //将Mat转换为Bitmap
    MatToBitmap(env,src,bitmapDest, JNI_FALSE);

    //释放Mat
    src.release();
    return bitmapDest;
}

extern "C"
JNIEXPORT jobject JNICALL
Java_com_itfitness_cppdemoone_activity_AddWeightActivity_imageAddWeight(JNIEnv *env, jobject thiz,
                                                                        jobject bitmap_src,
                                                                        jobject bitmap_src2) {
    //源图像
    Mat src,src2;
    //将Bitmap转换为Mat
    BitmapToMat(env,bitmap_src,src, JNI_FALSE);
    BitmapToMat(env,bitmap_src2,src2, JNI_FALSE);

    //将src2的大小调整为与src一样大
    AndroidBitmapInfo info;
    if(AndroidBitmap_getInfo(env, bitmap_src, &info) >= 0){
        resize(src2,src2,Size(info.width,info.height));
        //添加权重进行融合
        
        addWeighted(src,0.8,src2,0.2,0,src);
    }

    //将Mat转换为Bitmap
    MatToBitmap(env,src,bitmap_src, JNI_FALSE);

    //释放Mat
    src.release();
    return bitmap_src;
}

extern "C"
JNIEXPORT jobject JNICALL
Java_com_itfitness_cppdemoone_activity_CutoutActivity_imageCutout(JNIEnv *env, jobject thiz,
                                                                  jobject bitmap_src,jint minH,jint minS,jint minV,
                                                                  jint maxH,jint maxS,jint maxV) {
    Mat img;
    BitmapToMat(env,bitmap_src,img, JNI_FALSE);
//    img_back=cv2.imread('back.jpg')
//日常缩放
//    rows,cols,channels = img_back.shape
//    img_back=cv2.resize(img_back,None,fx=0.7,fy=0.7)
//    cv2.imshow('img_back',img_back)

//    rows,cols,channels = img.shape
//    img=cv2.resize(img,None,fx=0.4,fy=0.4)
//    cv2.imshow('img',img)
//    rows,cols,channels = img.shape#rows，cols最后一定要是前景图片的，后面遍历图片需要用到

//转换hsv
    cvtColor(img,img,COLOR_BGR2HSV);
//获取mask
//    lower_blue=np.array([78,43,46])
//    upper_blue=np.array([110,255,255])
    inRange(img,Scalar(minH,minS,minV),Scalar(maxH,maxS,maxV),img);
//腐蚀膨胀
    Mat kernel = getStructuringElement(MORPH_RECT,Size(3,3));//创建结构元素大小为3*3
//    erode(img,img,kernel);
//    dilate(img,img,kernel);//膨胀操作

    MatToBitmap(env,img,bitmap_src, JNI_FALSE);
    return bitmap_src;
}

extern "C"
JNIEXPORT jobject JNICALL
Java_com_itfitness_cppdemoone_activity_EdgeCuttingActivity_imageEdgeCutting(JNIEnv *env,
                                                                            jobject thiz,
                                                                            jobject bitmap_src) {
    //源图像
    Mat src,src_copy;
    //将Bitmap转换为Mat
    BitmapToMat(env,bitmap_src,src, JNI_FALSE);
    //复制
    src.copyTo(src_copy);
    //转换为灰度图
    cvtColor(src_copy,src_copy,COLOR_BGR2GRAY);
    //图像二值化
    threshold(src_copy,src_copy,120,250,THRESH_BINARY);
    //轮廓发现
    vector<vector<Point>> contours;
    vector<Vec4i> hierarchy;
    //检测外围轮廓
    findContours(src_copy,contours, hierarchy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE,Point(0,0));//轮廓检测

    //找出最大轮廓
    Rect rectMax;
    int i;
    for(i = 0 ; i < contours.size() ; i ++){
        Rect rect = boundingRect(contours[i]);
        if(rect.width * rect.height > rectMax.width * rectMax.height){
            rectMax.width = rect.width;
            rectMax.height = rect.height;
            rectMax.x = rect.x;
            rectMax.y = rect.y;
            LOGE("轮廓","宽：%d,高：%d,位置：%d,%d",rectMax.width,rectMax.height,rectMax.x,rectMax.y);
        }
    }
    //调用Activity中的方法进行裁剪
    jclass clazz = env->GetObjectClass(thiz);
    jmethodID  jmethodId = env->GetMethodID(clazz,"createClipBitmap",
                     "(Landroid/graphics/Bitmap;IIII)Landroid/graphics/Bitmap;");
    jobject destBitmap = env->CallObjectMethod(thiz,jmethodId,bitmap_src,rectMax.x,rectMax.y,rectMax.width,rectMax.height);
    //释放Mat
    src.release();
    src_copy.release();
    return destBitmap;
}
extern "C"
JNIEXPORT jobject JNICALL
Java_com_itfitness_cppdemoone_activity_HistogramAverageActivity_imageHistogramAverage(JNIEnv *env,
                                                                                      jobject thiz,
                                                                                      jobject bitmap_src) {
    //源图像
    Mat src;
    //将Bitmap转换为Mat
    BitmapToMat(env,bitmap_src,src, JNI_FALSE);

    cvtColor(src,src,CV_BGR2GRAY);

    //直方图均值化
    equalizeHist(src, src);//图像直方图均衡化

    //将Mat转换为Bitmap
    MatToBitmap(env,src,bitmap_src, JNI_FALSE);
    //释放Mat
    src.release();
    return bitmap_src;
}
extern "C"
JNIEXPORT jobject JNICALL
Java_com_itfitness_cppdemoone_activity_HistogramAverageActivity_imageAdaptiveHistogramAverage(
        JNIEnv *env, jobject thiz, jobject bitmap_src) {
    //源图像
    Mat src;
    //将Bitmap转换为Mat
    BitmapToMat(env,bitmap_src,src, JNI_FALSE);

    cvtColor(src,src,CV_BGR2GRAY);

    //自适应直方图均值化
    Ptr<CLAHE> ptr = createCLAHE(2.0,Size(8,8));
    ptr->apply(src,src);

    //将Mat转换为Bitmap
    MatToBitmap(env,src,bitmap_src, JNI_FALSE);
    //释放Mat
    src.release();
    return bitmap_src;
}